package com.zerdoor.controller;

import com.zerdoor.pagehelper.PageHelper;
import com.zerdoor.pagehelper.PageInfo;
import com.zerdoor.service.AdvertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Project: acgplat
 * Module ID: 002
 * Comments: 广告管理
 * JDK version used: JDK1.7
 * Namespace: controller
 * Author：xiaoji
 * Create Date：2014-11-24
 * Modified By：   <修改人中文名或拼音缩写>
 * Modified Date:  <修改日期，格式:YYYY-MM-DD>
 * Why & What is modified  <修改原因描述>
 * Version:                  <版本号>
 */
@Controller
@RequestMapping("advert")
public class AdvertController {

    @Autowired
    private AdvertService advertService;

    /**
     * FunName:           index
     * Description :      广告列表跳转
     * @return String：返回广告列表页
     * @Author:       xiaoji
     * @Create Date: 2014-11-24
     */
    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(){return "/advert/index";}

    /**
     * FunName:           getAdverts
     * Description :      获取广告列表
     * @return String：返回广告数据
     * @Author:       xiaoji
     * @Create Date: 2014-11-24
     */
    @RequestMapping(value = "/getAdverts",method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getAdverts(HttpServletRequest request,int page,int rows){

        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, String> params = new HashMap<String, String>();
        PageHelper.startPage(page, rows);
        params.put("title",request.getParameter("title"));
        params.put("advStatus",request.getParameter("advStatus"));
        params.put("advGroup",request.getParameter("advGroup"));
        List<Map<String, Object>> list = advertService.selectAllAdverts(params);
        PageInfo pageInfo = new PageInfo(list);
        result.put("rows",list);
        result.put("total",pageInfo.getTotal());
        return result;
    }

    /**
     * FunName:           addAdverts
     * Description :      添加广告
     * @return String：返回添加广告结果
     * @Author:       xiaoji
     * @Create Date: 2014-11-24
     */
    @RequestMapping(value = "/addAdverts",method = RequestMethod.POST)
    @ResponseBody
    public void addAdverts(HttpServletRequest request,HttpServletResponse response) throws Exception{
        Map<String, String> params = new HashMap<String, String>();
        params.put("advertId",null);
        params.put("title",request.getParameter("title"));
        params.put("imgurl",request.getParameter("imgurl"));
        params.put("url",request.getParameter("url"));
        params.put("advInterval",request.getParameter("advInterval"));
        params.put("advStatus",request.getParameter("advStatus"));
        params.put("advGroup",request.getParameter("advGroup"));

        Map<String, Object> result = new HashMap<String, Object>();
        try {
            advertService.addAdvert(params);
        } catch (Exception e) {
            result.put("errorMsg", "添加广告失败！");
        }
        PrintWriter out = response.getWriter();
        out.write(result.toString());
        out.flush();
    }

    /**
     * FunName:           updateAdvert
     * Description :      修改广告
     * @return String：返回修改广告结果
     * @Author:       xiaoji
     * @Create Date: 2014-11-24
     */
    @RequestMapping(value = "/updateAdvert",method = RequestMethod.POST)
    @ResponseBody
    public void updateAdvert(HttpServletRequest request,HttpServletResponse response) throws Exception{
        Map<String, String> params = new HashMap<String, String>();
        params.put("advertId",request.getParameter("id"));
        params.put("title",request.getParameter("title"));
        params.put("imgurl",request.getParameter("imgurl"));
        params.put("url",request.getParameter("url"));
        params.put("advInterval",request.getParameter("advInterval"));
        params.put("advStatus",request.getParameter("advStatus"));
        params.put("advGroup",request.getParameter("advGroup"));

        Map<String, Object> result = new HashMap<String, Object>();
        try {
            advertService.updateAdvert(params);
        } catch (Exception e) {
            result.put("errorMsg", "编辑广告失败！");
        }

        PrintWriter out = response.getWriter();
        out.write(result.toString());
        out.flush();
    }

    /**
     * FunName:           updateAdvertInit
     * Description :      根据ID获取广告对象
     * @return String：返回广告
     * @Author:       xiaoji
     * @Create Date: 2014-11-24
     */
    @RequestMapping(value = "/updateAdvertInit",method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> updateAdvertInit(HttpServletRequest request){
        Map<String, String> params = new HashMap<String, String>();
        params.put("advertId",request.getParameter("advertId"));
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> map = advertService.selectAdvertById(params);
        result.put("advert", map);
        return map;
    }

    /**
     * FunName:           deleteAdvert
     * Description :      删除广告
     * @return String：删除广告结果
     * @Author:       xiaoji
     * @Create Date: 2014-11-24
     */
    @RequestMapping(value = "/deleteAdvert",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteAdvert(HttpServletRequest request){
        Map<String, String> params = new HashMap<String, String>();
        params.put("advertId",request.getParameter("id"));
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            advertService.deleteAdvert(params);
        } catch (Exception e) {
            result.put("errorMsg", "删除广告失败！");
        }
        return result;
    }

    /**
     * FunName:           getAdvertGroup
     * Description :      获取广告分组
     * @return String：广告分组信息
     * @Author:       xiaoji
     * @Create Date: 2014-11-25
     */
    @RequestMapping(value = "/getAdvertGroup",method = RequestMethod.GET)
    @ResponseBody
    public List<Map<String, Object>> getAdvertGroup(){
        Map<String, Object> map = new HashMap<String, Object>();
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        map.put("id", "");
        map.put("text", "全部");
        map.put("selected", true);
        list.add(map);
        list.addAll(advertService.selectAdvertGroup());
        return list;
    }
}
